import {useState} from "react";
import {fileHash} from "@/tools/tools";
import {classificationImagesPost} from "@/services/ant-design-pro/marketing";
import {Button, message, Upload} from "antd";

const UploadPictures = () => {
  // 上传图片
  const [host,setHost] = useState('')
  const [ossData,setOssData] = useState({accessId:'', policy:'', signature:''})
  const uploadPic = {
    showUploadList:false,
    beforeUpload: async (file) => {
      let hash = await fileHash(file)

      let item = {
        name:file.name,
        size:file.size,
        type:file.type,
        hash:hash,
        upload_type:'oss',
        module_type:'admin',
        original_name:file.name
      }
      let ossDataItem = await classificationImagesPost(item)
      setOssData(ossDataItem)
      setHost(ossData.host)
      file.url =  hash;
      return file
    },
    onChange:({file})=>{
      if(file.status == 'done'){
        message.success('上传成功')
      }
    },
    data:file => {
      return {
        key: file.url,
        OSSAccessKeyId: ossData.accessid,
        policy: ossData.policy,
        Signature: ossData.signature,
      };
    },

  };
  return (
    <Upload   action={'https://wm-shop-api.oss-cn-beijing.aliyuncs.com/'} {...uploadPic}>
      <Button type='primary'>上传图片</Button>
    </Upload>
  );
};

export default UploadPictures;
